package com.small.nine.wxmp.domain.bean.wx.msg;

import com.small.nine.wxmp.common.constant.WxConstant;
import com.small.nine.wxmp.utils.wx.XmlUtils;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 回复图文消息
 *
 * @author sheng_zs@126.com
 * @date 2021-08-02 17:53
 */
@Data
@EqualsAndHashCode(callSuper = true)
@XStreamAlias("xml")
public class WxNewsMsgBean extends WxOutMsgBaseBean {
    private static final long serialVersionUID = 1L;

    /**
     * 图文消息个数；当用户发送文本、图片、语音、视频、图文、地理位置这六种消息时，开发者只能回复1条图文消息；其余场景最多可回复8条图文消息
     */
    @XStreamAlias("ArticleCount")
    private Integer articleCount;

    /**
     * Articles节点，图文消息信息，注意，如果图文数超过限制，则将只发限制内的条数
     */
    @XStreamAlias("Articles")
    private List<ArticlesBean> articles;

    @Override
    public String toXml() {
        return XmlUtils.beanToXml(this);
    }

    /**
     * 初始化
     *
     * @param toUser        接收者，openID
     * @param fromUser      发送者
     * @param createTime    时间戳
     * @param articlesBeans Articles节点，最多三个节点
     * @return WxNewsMsgBean
     */
    public static WxNewsMsgBean getInstance(String toUser, String fromUser, Long createTime, ArticlesBean... articlesBeans) {
        WxNewsMsgBean msgBean = new WxNewsMsgBean();
        msgBean.setToUserName(toUser);
        msgBean.setFromUserName(fromUser);
        msgBean.setMsgType(WxConstant.MSG_TYPE_NEWS);
        msgBean.setCreateTime(createTime);
        // 图文信息
        if (Objects.nonNull(articlesBeans) && articlesBeans.length > 0) {
            msgBean.setArticleCount(articlesBeans.length);
            msgBean.setArticles(new ArrayList<>(Arrays.asList(articlesBeans)));
        }
        return msgBean;
    }
}
